﻿using QuanLyBanHang.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyBanHang.DAL
{
   public class HoaDonDAL
    {

        private static KetNoiSQL kn = new KetNoiSQL();
        private static SqlCommand cmd = new SqlCommand();
        public static int ThemHoaDon(HoaDonDTO hd)
        {
            cmd.CommandText = "insert into ChiTietHD values (@MaHD,@MaKH,@MaNV,@NgayLapHD)";
            cmd.Parameters.Add("@MaHD", SqlDbType.NVarChar, 50).Value = HoaDonDTO.MaHD;
            cmd.Parameters.Add("@MaKH", SqlDbType.NVarChar, 50).Value = HoaDonDTO.MaKH;
            cmd.Parameters.Add("@MaNV", SqlDbType.NVarChar,50).Value = HoaDonDTO.MaNV;
            cmd.Parameters.Add("@NgayLapHD", SqlDbType.DateTime).Value = HoaDonDTO.NgayLapHD;
            return kn.DongThucThi(cmd);
        }
        public static int XoaHD(HoaDonDTO hd)
        {

            cmd.CommandText = "delete ChiTietHD where MaHD=@MaHD and MaKH = @MaKH";
            cmd.Parameters.Add("@MaHD", SqlDbType.NVarChar, 50).Value = HoaDonDTO.MaHD;
            cmd.Parameters.Add("@MaKH", SqlDbType.NVarChar, 50).Value = HoaDonDTO.MaKH;

            return kn.DongThucThi(cmd);
        }
        public static int SuaHD(HoaDonDTO hd)
        {

            cmd.CommandText = "update ChiTietHD set MaNV=@MaNV,NgayLapHD=@NgayLapHD where  MaHD=@MaHD and MaKH = @MaKH ";

            cmd.Parameters.Add("@MaNV", SqlDbType.NVarChar,50).Value = HoaDonDTO.MaNV;
            cmd.Parameters.Add("@NgayLapHD", SqlDbType.DateTime).Value = HoaDonDTO.NgayLapHD;
          
            return kn.DongThucThi(cmd);
        }
        public static DataTable LayTatCaThongTinHoaDon(HoaDonDTO hd)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from HoaDon";
            return kn.LayDuLieu(cmd);

        }
      
        //load du lieu len dataGRIDVIEW input la dieu kien
        public static DataTable LayTTHoaDonTheoDK(string dieukien)
        {
            return KetNoiSQL.LayDuLieuSelect("select * from HoaDon " + dieukien);
        }
        //lay ma san phan 
        public static DataTable LayThongTinKhachHang(string dieukien)
        {
            return KetNoiSQL.LayDuLieuSelect("select MaKH,TenSP from KhachHang " + dieukien);
        }
       //lay tt nhan vien
        public static DataTable LayThongTinNhanVien(string dieukien)
        {
            return KetNoiSQL.LayDuLieuSelect("select MaNV,TenNV from NhanVien " + dieukien);
        }
       
    }
}
